package com.ygk.bluepath.notification.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

/**
 * @author  李文伟 E-mail: handreace@163.com
 * @date 创建时间：2016年6月30日 下午5:03:15
 * @version 1.0
 */
public class GpsAndNetWorkStateReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		 if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
	            Toast.makeText(context, "GPS 状态："+gPSIsOPen(context),
	            Toast.LENGTH_SHORT).show();
	       
	        }else if (intent.getAction().matches("android.net.conn.CONNECTIVITY_CHANGE")) {
	            Toast.makeText(context, "网络 状态："+getNetState(context),
	    	            Toast.LENGTH_SHORT).show();
			}
	}
	private String getNetState(Context context) {
		// TODO Auto-generated method stub
		String rString="";
		ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		  if(connectMgr.getActiveNetworkInfo() != null) {  
				rString+="Available--"+connectMgr.getActiveNetworkInfo().isAvailable();  
		    }else {
		    	rString+="Available--null";  
			}  
		rString+="\n mobileNet--"+mobNetInfo.isConnected();
		rString+="\n wifiNet--"+wifiNetInfo.isConnected();
		return rString;
	}
	/**
     * 判断GPS是否开启，GPS或者AGPS开启一个就认为是开启的
     * @param context
     * @return true 表示开启
     */
    public static final boolean gPSIsOPen(final Context context) {
        LocationManager locationManager
                = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        // 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // 通过WLAN或移动网络(3G/2G)确定的位置（也称作AGPS，辅助GPS定位。主要用于在室内或遮盖物（建筑群或茂密的深林等）密集的地方定位）
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps || network) {
            return true;
        }
        return false;
    }
}
